/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.NewActionHelp;
import com.ciko.code.bean.UserBean;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000204.R;
import com.shwread.android.ui.dialog.NetworkDialog;
import com.shwread.http.base.TextHttpResponseHandler;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.apache.http.Header;

import base.tina.core.task.infc.ITaskResult;
import logic.util.NetworkUtil;
import logic.util.Util;


public class PersonalChangeUserInfoPage
        extends
        AbstractUIPage<MainActivity>
        implements
        OnClickListener {
    public static final String TYPE_HOMETOWN = "家乡";
    public static final String TYPE_LIVENOW = "现居地";
    public static final String TYPE_JOB = "职业";
    public static final String TYPE_INTEREST = "兴趣";
    public static final String TYPE_DESC = "一句家乡话";

    private TextView tvTitle;
    private Button btnBack;
    private EditText etInfo;
    private Button btnSubmit;

    private UserBean user;
    private InputMethodManager inputMethodManager;

    public PersonalChangeUserInfoPage(MainActivity t, UserBean user) {
        super(t);
        this.user = user;
    }

    private String lable, defaultValue;

    public void setLable(String lable, String defaultValue) {
        this.lable = lable;
        this.defaultValue = defaultValue;
    }

    @Override
    public <E extends Activity> View createView(E e, int initializers) {
        if (null != curMyView) return curMyView;
        curMyView = bActivity.getLayoutInflater().inflate(R.layout.user_info_setting_change_userinfo, null);
        inputMethodManager = (InputMethodManager) bActivity.getSystemService(Context.INPUT_METHOD_SERVICE);

        initView();
        setListener();

        return curMyView;
    }

    void initView() {
        tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
        tvTitle.setText("修改" + lable);
        btnBack = (Button) curMyView.findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);
        etInfo = (EditText) curMyView.findViewById(R.id.user_info_setting_change_userinfo_et);
        btnSubmit = (Button) curMyView.findViewById(R.id.user_info_setting_change_userinfo_submit_tv);
        etInfo.setHint(lable);
        if (!TextUtils.isEmpty(defaultValue)) {
            etInfo.setText(defaultValue);
            etInfo.setSelection(etInfo.getText().length());
        }
    }

    void setListener() {
        btnBack.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public View updateView(int toStatus, ITaskResult data) {
        return null;
    }

    @Override
    public void notifyView(int cmd, Bundle bundle) {

    }

    @Override
    public int enter(IUIPage<?> prePage) {
        etInfo.requestFocus();
        inputMethodManager.showSoftInput(etInfo, InputMethodManager.RESULT_UNCHANGED_SHOWN);
        return 0;
    }

    @Override
    public int leave(IUIPage<?> nextPage) {
        Util.hideInputMethodManager(context, etInfo);
        return 0;
    }

    @Override
    public void setStatus(int status) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_back:
                bActivity.onBackPressed();
                break;
            case R.id.user_info_setting_change_userinfo_submit_tv:
                submit();
                break;
            default:
                break;
        }
    }

    private String infoContent;

    private void submit() {
        infoContent = etInfo.getText().toString().trim();
        if (NetworkUtil.isNetAvailable(bActivity)) {
            if (Util.isNotEmpty(infoContent)) {
                request();
            } else {
                Util.showToast(bActivity, "请输入" + lable);
            }
        } else {
            new NetworkDialog(bActivity).show();
        }
    }

    private void request() {
        if (TextUtils.isEmpty(lable)) return;
        if (lable.equals(TYPE_HOMETOWN)) {
            NewActionHelp.modUserAccountInfo(context, null, null, null, null, null, null, infoContent, null, null, null, null, new InfoHandler());
        } else if (lable.equals(TYPE_LIVENOW)) {
            NewActionHelp.modUserAccountInfo(context, null, null, null, null, null, null, null, infoContent, null, null, null, new InfoHandler());
        } else if (lable.equals(TYPE_INTEREST)) {
            NewActionHelp.modUserAccountInfo(context, null, null, null, null, null, null, null, null, infoContent, null, null, new InfoHandler());
        } else if (lable.equals(TYPE_JOB)) {
            NewActionHelp.modUserAccountInfo(context, null, null, null, null, null, null, null, null, null, infoContent, null, new InfoHandler());
        } else if (lable.equals(TYPE_DESC)) {
            NewActionHelp.modUserAccountInfo(context, null, null, null, null, null, null, null, null, null, null, infoContent, new InfoHandler());
        }
    }

    class InfoHandler extends TextHttpResponseHandler {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            Util.showToast(bActivity, "保存失败，请稍候再试");
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            if (lable.equals(TYPE_HOMETOWN)) {
                user.setHomeTown(infoContent);
            } else if (lable.equals(TYPE_LIVENOW)) {
                user.setLiveNow(infoContent);
            } else if (lable.equals(TYPE_INTEREST)) {
                user.setInterest(infoContent);
            } else if (lable.equals(TYPE_JOB)) {
                user.setJob(infoContent);
            } else if (lable.equals(TYPE_DESC)) {
                user.setDesc(infoContent);
            }
            Util.showToast(bActivity, "保存成功");
            bActivity.onBackPressed();
        }

    }
}
